<?php

namespace App\Services;

use App\Models\CourseModel;

class CourseService extends BaseService {

    public function list($params) {
        $home = data_get($params, 'home', null);
        $page = data_get($params, 'page', 0);
        $limit = data_get($params, 'limit',20);

        $query = CourseModel::query();

        $query->orderBy('id', 'desc');
        $res = $query->paginate($limit, CourseModel::$columns, 'page', $page)->toArray();
        if ($home == 1){
            return $res;
        }else {
            return [
                'page' => $res['total'],
                'list' => $res['data']
            ];
        }
    }

    public function up_down($info_id, $type="up"){
        if ($type == "up"){
            $res = CourseModel::query()->where("id", "<",  $info_id)->first();
        }elseif ($type == "down"){
            $res = CourseModel::query()->where("id", ">", $info_id)->first();
        }

        return $res ? $res->toArray() : [];
    }

    public function info($info_id) {
        $res = CourseModel::query()->where("id", $info_id)->first();
        return $res ? $res->toArray() : [];
    }

    public function delete($info_id) {
        return CourseModel::query()->where("id", $info_id)->delete();
    }
}